package com.hb.trade.server.center.au.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.center.au.dto.UserVerifyDTO;
import com.hb.core.pojo.center.au.entity.User;
import com.hb.core.pojo.center.au.entity.UserVerify;
import com.hb.trade.server.center.au.service.UserVerifyService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户认证表, 用户注册时的认证方式 前端控制器
 * </p>
 *
 * @author ljs
 * @since 2022-3-23 16:53:14
 */
@RestController
public class UserVerifyController {
    private final UserVerifyService userVerifyService;

    public UserVerifyController(UserVerifyService userVerifyService) {
        this.userVerifyService = userVerifyService;
    }

    /**
     * 获取所有用户信息
     *
     * @param verifyCode
     * @return
     */
    @PostMapping("/userVerify/getUserIdByVerifyCode")
    public Long getUserIdByVerifyCode(@RequestBody String verifyCode) {
        QueryWrapper<UserVerify> wrapper = new QueryWrapper<>();
        wrapper.eq("verify_code", verifyCode);
        wrapper.last("limit 1");
        UserVerify userVerify = userVerifyService.getOne(wrapper);
        if (userVerify!=null){
            return userVerify.getUserId();
        }
        return null;
    }

    /**
     * 获取根据用户名和交易码获取绑定信息
     *
     * @param user
     * @return
     */
    @PostMapping("/userVerify/getUserVerifyByUserId")
    public Message<UserVerify> getUserVerifyByUserId(@RequestBody User user){
        QueryWrapper<UserVerify> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getId()).eq("verify_type", 2);
        wrapper.last("limit 1");
        UserVerify userVerify = userVerifyService.getOne(wrapper);
        return MsgUtil.success(userVerify);
    }


    /**
     * 保存用户CA信息
     *
     * @param userVerify
     * @return
     */
    @PostMapping("/userVerify/saveVerifyByZapp")
    public Message<Boolean> saveVerifyByZapp(@RequestBody UserVerify userVerify){
        // 判断是否已经绑定的信息
        QueryWrapper<UserVerify> wrapper = new QueryWrapper<>();
        wrapper.eq("verify_code", userVerify.getVerifyCode());
        wrapper.eq("user_id", userVerify.getUserId());
        wrapper.last("limit 1");
        UserVerify verify = userVerifyService.getOne(wrapper);
        boolean result;
        if (verify == null){
            userVerify.buildBaseData(1);
            result = userVerifyService.save(userVerify);
        }else {
            userVerify.setId(verify.getId());
            userVerify.buildBaseData(2);
            result = userVerifyService.updateById(userVerify);
        }

        return MsgUtil.success(result);
    }

    /**
     * 根据用户id查询绑定的第三方ca或者中招
     * @param userVerify
     * @return
     */
    @PostMapping("/userVerify/listVerifyByUserId")
    public Message<List<UserVerify>> listVerifyByUserId(@RequestBody UserVerify userVerify){
        QueryWrapper<UserVerify> wrapper = new QueryWrapper<>();
        wrapper.select("id","verify_type", "audit_status");
        wrapper.eq("user_id", userVerify.getUserId()).eq("audit_status", 1);
        List<UserVerify> userVerifyList = userVerifyService.list(wrapper);
        return MsgUtil.success(userVerifyList);
    }

    /**
     * 获取认证信息
     * @param userVerifyDTO
     * @return
     */
    @PostMapping("/userVerify/findVerifyInfo")
    public Message<List<UserVerifyDTO>> findVerifyInfo(@RequestBody UserVerifyDTO userVerifyDTO) {
        QueryWrapper<UserVerify> wrapper = new QueryWrapper<>();
        if (userVerifyDTO.getUserId() != null) {
            wrapper.eq("user_id", userVerifyDTO.getUserId());
        }
        if (userVerifyDTO.getVerifyType() != null) {
            wrapper.eq("verify_type", userVerifyDTO.getVerifyType());
        }
        if (userVerifyDTO.getAuditStatus() != null) {
            wrapper.eq("audit_status", userVerifyDTO.getAuditStatus());
        }
        List<UserVerify> verifyList = userVerifyService.list(wrapper);

        List<UserVerifyDTO> userVerifyDTOList = new ArrayList<>();
        for (UserVerify userVerify : verifyList) {
            UserVerifyDTO verifyDTO = BeanUtil.copyProperties(userVerify, UserVerifyDTO.class);
            userVerifyDTOList.add(verifyDTO);
        }

        return MsgUtil.success(userVerifyDTOList);
    }


    /**
     * 获取中招token是否失效（有效期10天）
     *
     * @author tian
     * @date 2022-6-16 16:58
     * @param userId
     * @return org.fzzn.core.model.msg.Message<java.lang.Boolean>
     */
    @PostMapping("/userVerify/judgeZZToken")
    public Message<Boolean> judgeZZToken(@RequestBody Long userId) {
        QueryWrapper<UserVerify> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.last("limit 1");
        UserVerify userVerify = userVerifyService.getOne(wrapper);

        // 获取token最后更新时间
        LocalDateTime finalTime = null != userVerify.getUpdateTime() ? userVerify.getUpdateTime() : userVerify.getCreateTime();

        int daysNum = (int) (LocalDateTime.now().toLocalDate().toEpochDay() - finalTime.toLocalDate().toEpochDay());

        if (daysNum > 10) {
            return MsgUtil.success(false);
        }
        return MsgUtil.success(true);
    }
}
